home *** CD-ROM | disk | FTP | other *** search
- Imports System.Drawing.Imaging
-
- Public Class Imaging
- Inherits System.Windows.Forms.Form
-
- #Region " Windows Form Designer generated code "
-
- Public Sub New()
- MyBase.New()
-
- 'This call is required by the Windows Form Designer.
- InitializeComponent()
-
- 'Add any initialization after the InitializeComponent() call
-
- End Sub
-
- 'Form overrides dispose to clean up the component list.
- Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
- If disposing Then
- If Not (components Is Nothing) Then
- components.Dispose()
- End If
- End If
- MyBase.Dispose(disposing)
- End Sub
- Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu
- Friend WithEvents MenuItem1 As System.Windows.Forms.MenuItem
- Friend WithEvents mnuLoad As System.Windows.Forms.MenuItem
- Friend WithEvents OpenFileDialog1 As System.Windows.Forms.OpenFileDialog
- Friend WithEvents mnuSave As System.Windows.Forms.MenuItem
- Friend WithEvents SaveFileDialog1 As System.Windows.Forms.SaveFileDialog
- Friend WithEvents mnuClipping As System.Windows.Forms.MenuItem
- Friend WithEvents mnuRotate As System.Windows.Forms.MenuItem
- Friend WithEvents mnuTransparentBitmaps As System.Windows.Forms.MenuItem
- Friend WithEvents mnuIcons As System.Windows.Forms.MenuItem
- Friend WithEvents mnuMetafiles As System.Windows.Forms.MenuItem
- Friend WithEvents mnuDrawMetafile As System.Windows.Forms.MenuItem
-
- 'Required by the Windows Form Designer
- Private components As System.ComponentModel.Container
-
- 'NOTE: The following procedure is required by the Windows Form Designer
- 'It can be modified using the Windows Form Designer.
- 'Do not modify it using the code editor.
- <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
- Me.mnuClipping = New System.Windows.Forms.MenuItem()
- Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog()
- Me.mnuLoad = New System.Windows.Forms.MenuItem()
- Me.MainMenu1 = New System.Windows.Forms.MainMenu()
- Me.MenuItem1 = New System.Windows.Forms.MenuItem()
- Me.mnuSave = New System.Windows.Forms.MenuItem()
- Me.mnuRotate = New System.Windows.Forms.MenuItem()
- Me.mnuTransparentBitmaps = New System.Windows.Forms.MenuItem()
- Me.mnuIcons = New System.Windows.Forms.MenuItem()
- Me.mnuMetafiles = New System.Windows.Forms.MenuItem()
- Me.SaveFileDialog1 = New System.Windows.Forms.SaveFileDialog()
- Me.mnuDrawMetafile = New System.Windows.Forms.MenuItem()
- '
- 'mnuClipping
- '
- Me.mnuClipping.Index = 2
- Me.mnuClipping.Text = "Clipping and scaling"
- '
- 'mnuLoad
- '
- Me.mnuLoad.Index = 0
- Me.mnuLoad.Text = "Load from file"
- '
- 'MainMenu1
- '
- Me.MainMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem1})
- '
- 'MenuItem1
- '
- Me.MenuItem1.Index = 0
- Me.MenuItem1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnuLoad, Me.mnuSave, Me.mnuClipping, Me.mnuRotate, Me.mnuTransparentBitmaps, Me.mnuIcons, Me.mnuMetafiles, Me.mnuDrawMetafile})
- Me.MenuItem1.Text = "Examples"
- '
- 'mnuSave
- '
- Me.mnuSave.Index = 1
- Me.mnuSave.Text = "Save to File"
- '
- 'mnuRotate
- '
- Me.mnuRotate.Index = 3
- Me.mnuRotate.Text = "Mirror, Rotate, and Skew"
- '
- 'mnuTransparentBitmaps
- '
- Me.mnuTransparentBitmaps.Index = 4
- Me.mnuTransparentBitmaps.Text = "Semi-transparent bitmaps"
- '
- 'mnuIcons
- '
- Me.mnuIcons.Index = 5
- Me.mnuIcons.Text = "Icons"
- '
- 'mnuMetafiles
- '
- Me.mnuMetafiles.Index = 6
- Me.mnuMetafiles.Text = "Create Metafile"
- '
- 'SaveFileDialog1
- '
- Me.SaveFileDialog1.FileName = "doc1"
- '
- 'mnuDrawMetafile
- '
- Me.mnuDrawMetafile.Index = 7
- Me.mnuDrawMetafile.Text = "Draw Metafile"
- '
- 'Imaging
- '
- Me.AutoScaleBaseSize = New System.Drawing.Size(7, 17)
- Me.ClientSize = New System.Drawing.Size(664, 365)
- Me.Font = New System.Drawing.Font("Microsoft Sans Serif", 11!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
- Me.Menu = Me.MainMenu1
- Me.Name = "Imaging"
- Me.Text = "Imaging"
-
- End Sub
-
- #End Region
-
- Private Sub mnuLoad_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuLoad.Click
- With OpenFileDialog1
- .Filter = "Image files|*.bmp;*.jpg;*.jpeg;*.gif;*.png;*.exif;*.tif;*.tiff"
- If .ShowDialog = DialogResult.OK Then
- Dim bmp As Bitmap = Bitmap.FromFile(.FileName)
- Dim g As Graphics = Graphics.FromImage(bmp)
- g.FillRectangle(Brushes.Red, 20, 20, 100, 100)
- g.Dispose()
-
- Dim gr As Graphics = Me.CreateGraphics
-
- gr.DrawImage(bmp, 0, 0)
- bmp.Dispose()
- gr.Dispose()
- End If
- End With
- End Sub
-
- Private Sub mnuSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuSave.Click
- Dim bmp As Bitmap
- Dim gr As Graphics = Me.CreateGraphics
-
- With OpenFileDialog1
- .Title = "Select source image"
- .Filter = "Image files|*.bmp;*.jpg;*.jpeg;*.gif;*.png;*.exif;*.tif;*.tiff"
- If .ShowDialog <> DialogResult.OK Then Exit Sub
-
- bmp = New Bitmap(.FileName)
- gr.DrawImage(bmp, 0, 0)
- End With
-
- With SaveFileDialog1
- .Title = "Select target image and format"
- .Filter = "Bitmap|*.bmp|GIF|*.gif|TIFF|*.tif"
- .OverwritePrompt = True
- If .ShowDialog = DialogResult.OK Then
- Select Case System.IO.Path.GetExtension(.FileName).ToUpper
- Case ".BMP"
- bmp.Save(.FileName, ImageFormat.Bmp)
- Case ".GIF"
- bmp.Save(.FileName, ImageFormat.Gif)
- Case ".TIF"
- bmp.Save(.FileName, ImageFormat.Tiff)
- Case Else
- MessageBox.Show("Unrecognized extension", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
- End Select
- End If
- End With
-
- bmp.Dispose()
- gr.Dispose()
- End Sub
-
- Private Sub mnuClipping_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuClipping.Click
- ' Wait until the menu closes.
- Threading.Thread.Sleep(400)
-
- Dim gr As Graphics = Me.CreateGraphics
- gr.Clear(Color.White)
- Dim bmp As New Bitmap("logo.bmp")
-
- ' Copy only the left half of the image.
- gr.DrawImage(bmp, 20, 20, New RectangleF(0, 0, bmp.Width / 2, bmp.Height), GraphicsUnit.Pixel)
-
- ' Create a destination rectangle 3 times as wide and twice as tall.
- Dim rect As New RectangleF(20, 120, bmp.Width * 3, bmp.Height * 2)
- ' Draw the enlarged bitmap.
- gr.DrawImage(bmp, rect)
-
- ' Move the destination rectangle down.
- Dim destrect As New RectangleF(20, 320, bmp.Width * 1.5, bmp.Height)
- ' Create a rectangle corresponding to the top-left quarter of the image.
- Dim sourceRect As New RectangleF(0, 0, bmp.Width / 2, bmp.Height / 2)
- ' Draw the portion of bitmap in the destination rectangle.
- gr.DrawImage(bmp, destRect, sourceRect, GraphicsUnit.Pixel)
-
- bmp.Dispose()
- gr.Dispose()
- End Sub
-
- Private Sub mnuRotate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuRotate.Click
- ' Wait until the menu closes.
- Threading.Thread.Sleep(400)
-
- Dim gr As Graphics = Me.CreateGraphics
- gr.Clear(Color.White)
- Dim bmp As New Bitmap("logo.bmp")
-
- DrawFlipImage(gr, bmp, 100, 20, False, False)
- DrawFlipImage(gr, bmp, 300, 20, True, False)
- DrawFlipImage(gr, bmp, 100, 120, False, True)
- DrawFlipImage(gr, bmp, 300, 120, True, True)
-
- DrawRotateImage(gr, bmp, 100, 220, 45)
- DrawRotateImage(gr, bmp, 300, 220, 90)
- DrawRotateImage(gr, bmp, 500, 220, 135)
-
- DrawSkewImage(gr, bmp, 100, 400, -50, 0)
- DrawSkewImage(gr, bmp, 300, 400, 0, 50)
- DrawSkewImage(gr, bmp, 500, 400, -50, 50)
-
- bmp.Dispose()
- gr.Dispose()
- End Sub
-
- Private Sub mnuTransparentBitmaps_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuTransparentBitmaps.Click
- ' Wait until the menu closes.
- Threading.Thread.Sleep(400)
-
- Dim gr As Graphics = Me.CreateGraphics
- gr.Clear(Color.White)
- Dim bmp As New Bitmap("logo.bmp")
-
- ' Create a grid of lines.
- Dim p As New Pen(Color.Green, 4)
- Dim x, y As Single
- For x = 0 To Me.ClientSize.Width Step 40
- gr.DrawLine(p, x, 0, x, Me.ClientSize.Height)
- Next
- For y = 0 To Me.ClientSize.Height Step 40
- gr.DrawLine(p, 0, y, Me.ClientSize.Width, y)
- Next
-
- ' Create a clone bitmap and make it transparent.
- Dim bmp2 As Bitmap = CType(bmp.Clone(), Bitmap)
- bmp2.MakeTransparent(Color.FromArgb(140, 195, 247))
- gr.DrawImage(bmp2, 20, 20)
-
- ' Create a semi-transparent bitmap.
- Dim transparency As Single = 0.8
- ' Create a 5x5 matrix with the transparency value in position (4,4).
- Dim values()() As Single = {New Single() {1, 0, 0, 0, 0}, _
- New Single() {0, 1, 0, 0, 0}, _
- New Single() {0, 0, 1, 0, 0}, _
- New Single() {0, 0, 0, transparency, 0}, _
- New Single() {0, 0, 0, 0, 1}}
- ' Use the matrix to initialize a new colorMatrix object.
- Dim colMatrix As New ColorMatrix(values)
- ' Create an ImageAttributes object and assign its color matrix.
- Dim imageAttr As New ImageAttributes()
- imageAttr.SetColorMatrix(colMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap)
- ' Draw the bitmap using the specified color matrix.
- gr.DrawImage(bmp, New Rectangle(200, 20, bmp.Width, bmp.Height), 0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel, imageAttr)
-
- ' Change the alpha blending value of individual pixels.
- Dim bmp3 As Bitmap = bmp.Clone
- For x = 0 To bmp3.Width - 1
- For y = 0 To bmp3.Height - 1
- Dim oldColor = bmp3.GetPixel(x, y)
- Dim newColor As Color = Color.FromArgb(x / bmp3.Width * 256, oldColor)
- bmp3.SetPixel(x, y, newColor)
- Next
- Next
- gr.DrawImage(bmp3, 400, 20)
-
- bmp.Dispose()
- bmp2.Dispose()
- bmp3.Dispose()
- gr.Dispose()
-
- End Sub
-
- Private Sub mnuIcons_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuIcons.Click
- ' Wait until the menu closes.
- Threading.Thread.Sleep(400)
-
- Dim gr As Graphics = Me.CreateGraphics
- gr.Clear(Color.White)
-
- ' Load the icon from file.
- Dim icon As New Icon("W95MBX01.ICO")
- ' Draw the icon in its original size.
- gr.DrawIcon(icon, 20, 20)
- ' Draw the icon with a 400% zoom.
- gr.DrawIcon(icon, New Rectangle(100, 20, icon.Width * 4, icon.Height * 4))
-
- Dim bmp As Bitmap = icon.ToBitmap
- bmp.MakeTransparent(Color.Red)
- gr.DrawImage(bmp, 20, 200)
-
- icon.Dispose()
- bmp.Dispose()
- gr.Dispose()
- End Sub
-
- Private Sub mnuMetafiles_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuMetafiles.Click
- ' Wait until the menu closes.
- Threading.Thread.Sleep(400)
-
- Dim gr As Graphics = Me.CreateGraphics
-
- ' Create a new metafile object, with same features as the form's Graphics.
- Dim mf As New Metafile("sample.emf", gr.GetHdc)
- ' Retrieve the metafile's internal Graphics object.
- Dim mfgr As Graphics = Graphics.FromImage(mf)
- ' Draw some graphics and text on the metafile's Graphics.
- mfgr.DrawLine(Pens.Blue, 20, 20, 200, 200)
- mfgr.DrawRectangle(Pens.Red, 50, 50, 200, 120)
- mfgr.FillEllipse(Brushes.Green, 70, 20, 200, 120)
- Dim fnt As New Font("Arial", 20, FontStyle.Bold Or FontStyle.Italic)
- mfgr.DrawString("Test String ", fnt, Brushes.Yellow, 120, 100)
-
- ' End the drawing on the metafile's surface.
- fnt.Dispose()
- mfgr.Dispose()
- mfgr = Nothing
- mf.Dispose()
- mf = Nothing
-
- ' We must comment this statement out, otherwise we get an error
- ' "The object is in use elsewhere"
- ' gr.Dispose()
-
- MessageBox.Show("The Sample.emf file has been created")
- End Sub
-
- Private Sub mnuDrawMetafile_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuDrawMetafile.Click
- ' Wait until the menu closes.
- Threading.Thread.Sleep(400)
-
- Dim gr As Graphics = Me.CreateGraphics
- gr.Clear(Color.White)
-
- ' Read the metafile from file.
- Dim mf As New Metafile("sample.emf")
- ' Draw the metafile twice.
- gr.DrawImage(mf, 0, 0)
- gr.DrawImage(mf, 300, 100)
-
- mf.Dispose()
- gr.Dispose()
-
- End Sub
- End Class
-
-
-
-
- Module GraphicEffects
- Sub DrawFlipImage(ByVal gr As Graphics, ByVal bmp As Bitmap, ByVal x As Single, ByVal y As Single, ByVal flipX As Boolean, ByVal flipY As Boolean)
- ' Start with values of parallelogram's vertex as if no flipping occurs.
- Dim x0 As Single = x
- Dim y0 As Single = y
- Dim x1 As Single = x + bmp.Width
- Dim y1 As Single = y
- Dim x2 As Single = x
- Dim y2 As Single = y + bmp.Height
-
- ' Account for horizontal flipping.
- If flipX Then
- x0 = x + bmp.Width
- x1 = x
- x2 = x0
- End If
- ' Account for vertical flipping.
- If flipY Then
- y0 = y + bmp.Height
- y1 = y0
- y2 = y
- End If
- ' Create the points array.
- Dim points() As Point = {New Point(x0, y0), New Point(x1, y1), New Point(x2, y2)}
- ' Draw the flipped image
- gr.DrawImage(bmp, points)
- End Sub
-
- Sub DrawRotateImage(ByVal gr As Graphics, ByVal bmp As Bitmap, ByVal x As Single, ByVal y As Single, ByVal angle As Single)
- ' Convert the angle in degrees.
- angle = angle / (180 / Math.PI)
- ' Find the position of (x1,y1) and (x2,y2).
- Dim x1 As Single = x + bmp.Width * Math.Cos(angle)
- Dim y1 As Single = y + bmp.Width * Math.Sin(angle)
- Dim x2 As Single = x - bmp.Height * Math.Sin(angle)
- Dim y2 As Single = y + bmp.Height * Math.Cos(angle)
-
- ' Create the points array.
- Dim points() As Point = {New Point(x, y), New Point(x1, y1), New Point(x2, y2)}
- ' Draw the rotated image
- gr.DrawImage(bmp, points)
- End Sub
-
- Sub DrawSkewImage(ByVal gr As Graphics, ByVal bmp As Bitmap, ByVal x As Single, ByVal y As Single, ByVal dx As Single, ByVal dy As Single)
- ' Find the position of (x1,y1) and (x2,y2).
- Dim x1 As Single = x + bmp.Width
- Dim y1 As Single = y + dy
- Dim x2 As Single = x + dx
- Dim y2 As Single = y + bmp.Height
-
- ' Create the points array.
- Dim points() As Point = {New Point(x, y), New Point(x1, y1), New Point(x2, y2)}
- ' Draw the skewed image
- gr.DrawImage(bmp, points)
- End Sub
-
- Sub MakeBitmapSemiTransparent(ByVal bmp As Bitmap, ByVal transparency As Single)
-
- End Sub
- End Module
-